package com.example.musicplayer.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MusicListActivity;
import com.example.musicplayer.entity.MusicList;
import com.example.musicplayer.util.DbUtil;

import java.util.List;


public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {

    private List<MusicList> mMusicLists;
    @LayoutRes
    private int mResource; // 布局文件
    private Context context;
    private String mRequestCode;
    private String mMusicId;
    private DbUtil mDbUtil = new DbUtil();

    public void addData(int position, MusicList musicList) {
        mMusicLists.add(position, musicList);
        notifyItemInserted(position);
    }

    // 当requestcode为show时调用
    public MusicListAdapter(Context context, List<MusicList> mMusicLists, int mResource, String mRequestCode) {
        this.mMusicLists = mMusicLists;
        this.mResource = mResource;
        this.context = context;
        this.mRequestCode = mRequestCode;
    }

    // 当requestcode为action是调用，需要传入musicId用于添加歌曲到歌单
    public MusicListAdapter(Context context, List<MusicList> mMusicLists, @LayoutRes int resource, String requestCode, String musicId) {
        this.mMusicLists = mMusicLists;
        this.mResource = resource;
        this.context = context;
        this.mRequestCode = requestCode;
        this.mMusicId = musicId;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView musicListImg;
        TextView musicListTitle;
        TextView musicCount;
        ImageView moreVert;
        View musicListView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            musicListImg = itemView.findViewById(R.id.music_list_img);
            musicListTitle = itemView.findViewById(R.id.music_list_title);
            musicCount = itemView.findViewById(R.id.music_count);
            moreVert = itemView.findViewById(R.id.music_list_more_vert);
            musicListView = itemView;
        }

    }

    @NonNull
    @Override
    public MusicListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(mResource, parent, false);
        return new ViewHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull MusicListAdapter.ViewHolder holder, final int position) {
        final MusicList musicList = mMusicLists.get(position);
        Bitmap bitmap = mDbUtil.getMusicListCover(context, musicList.getName());
        holder.musicListImg.setImageBitmap(bitmap);
        holder.musicListTitle.setText(musicList.getName());
        String showMusicCount = "共" + musicList.getCount() + "首歌";
        holder.musicCount.setText(showMusicCount);
        holder.musicListView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRequestCode.equals("show")) {
                    Intent intent = new Intent(context, MusicListActivity.class);
                    intent.putExtra("title", musicList.getName());
                    intent.putExtra("request_code", "show");
                    intent.putExtra("musicId", "null");
                    context.startActivity(intent);
                } else if (mRequestCode.equals("action")) {
                    mDbUtil.addMusicToMusicList(context, mMusicId, musicList.getName());
                }

            }
        });
        holder.moreVert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(context, v);
                popupMenu.getMenuInflater().inflate(R.menu.music_list_menu, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        if (item.getItemId() == R.id.delete_music_list) {
                            boolean isDeleted = mDbUtil.deleteMusicList(context, mMusicLists.get(position).getName());
                            if (isDeleted) {
                                mMusicLists.remove(position);
                                notifyItemRemoved(position);
                                notifyItemRangeChanged(position, mMusicLists.size());
                            } else {
                                return true;
                            }
                        }
                        return true;
                    }
                });
                popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
                    @Override
                    public void onDismiss(PopupMenu menu) {
                    }
                });
                popupMenu.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMusicLists.size();
    }
}
